package cn.powertime.evaluation.app.controller;

import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.entity.CpsCredit;
import cn.powertime.evaluation.entity.CpsTestPaper;
import cn.powertime.evaluation.service.CpsCreditService;
import cn.powertime.evaluation.shiro.ShiroUtils;
import cn.powertime.evaluation.vo.*;
import cn.powertime.evaluation.web.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/cps-credit")
@Api(value = "/app/cps-credit", tags = "学分接口", produces = MediaType.ALL_VALUE)
public class AppCpsCreditController extends BaseController{


    @Autowired
    private CpsCreditService cpsCreditService;

    @PostMapping("/cps-credit")
    public  Object selectCredit(){
        QueryWrapper<CpsCredit>  queryWrapper = new QueryWrapper<>();
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        queryWrapper.eq("status", Constants.STATUS_NORMAL);
        queryWrapper.eq("user_id",userInfo.getId());
        List<CpsCredit> paperList= cpsCreditService.list(queryWrapper);
        Double crdit=0.00; Double f=0.00;
        for(int i=0;i<paperList.size();i++){
            f =paperList.get(i).getCredit();
            crdit=crdit+f;
        }
        CpsCreditVo cpsCreditVo = new CpsCreditVo();
        cpsCreditVo.setUserId(userInfo.getId());
        cpsCreditVo.setCredit(crdit);
        return  success(cpsCreditVo);
    }

    @PostMapping("/examine")
    public  Object examine(){
        QueryWrapper<CpsCredit>  queryWrapper = new QueryWrapper<>();
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        queryWrapper.eq("status",Constants.STATUS_NORMAL);
        queryWrapper.eq("user_id",userInfo.getId());
        List<CpsCredit> paperList= cpsCreditService.list(queryWrapper);
        CpsCreditClassifyVo cpsCreditClassifyVo=null;
        Double s=0.00; Double d=0.00;   Double s1=0.00; Double d1=0.00;  Double s2=0.00; Double d2=0.00;
        List<CpsCreditClassifyVo> vos= new ArrayList<>();
        for (int i=0;i<paperList.size();i++){
            if(paperList.get(i).getDict().equals(Constants.CREDIT_COURSE)){
               s= paperList.get(i).getCredit();
               d=d+s;
            }else if(paperList.get(i).getDict().equals(Constants.CREDIT__SHARE)){
                s1= paperList.get(i).getCredit();
                d1=d1+s1;
            }else {
                s2= paperList.get(i).getCredit();
                d2=d2+s2;
            }
        }
        cpsCreditClassifyVo=new CpsCreditClassifyVo();
        cpsCreditClassifyVo.setCredit(d);
        cpsCreditClassifyVo.setName(Constants.CREDIT_COURSE_CPE);
        cpsCreditClassifyVo.setId(1L);
        AppCreditVo vo = new AppCreditVo();
        vo.setUserId(userInfo.getId());
        vo.setDict(Constants.CREDIT_COURSE);
        List<WebChapterClassifyVo> chapterClassifyVo= cpsCreditService.selectChapter(vo);
        if(chapterClassifyVo.size()>0){
            cpsCreditClassifyVo.setChapterList(chapterClassifyVo);
        }
        vos.add(cpsCreditClassifyVo);
        cpsCreditClassifyVo= new CpsCreditClassifyVo();
        cpsCreditClassifyVo.setCredit(d1);
        cpsCreditClassifyVo.setName(Constants.CREDIT_SHARE_CPE);
        cpsCreditClassifyVo.setId(2L);
        vo.setDict(Constants.CREDIT__SHARE);
        List<WebChapterClassifyVo> chapterClassifyVos=  cpsCreditService.selectKnowledge(vo);
        if(chapterClassifyVos.size()>0){
            cpsCreditClassifyVo.setChapterList(chapterClassifyVos);
        }
        vos.add(cpsCreditClassifyVo);
        cpsCreditClassifyVo= new CpsCreditClassifyVo();
        cpsCreditClassifyVo.setCredit(d2);
        cpsCreditClassifyVo.setName(Constants.CREDIT_MATERIALS_CPE);
        cpsCreditClassifyVo.setId(2L);
        vo.setDict(Constants.CREDIT_MATERIALS);
        List<WebChapterClassifyVo> chapterClassifyVos1= cpsCreditService.selectMaterials(vo);
        if(chapterClassifyVos1.size()>0){
            cpsCreditClassifyVo.setChapterList(chapterClassifyVos1);
        }
        vos.add(cpsCreditClassifyVo);
        Double countCrdit=0.00;
        countCrdit=d+d1+d2;
        CpsCreditClassifyListVo listVo= new CpsCreditClassifyListVo();
        listVo.setCountCredit(countCrdit);
        listVo.setCreditList(vos);
        return  success(listVo);
    }

  /*  @PostMapping("/detail")
    public Object detailsCredit(@Validated @RequestBody WebDetailVo vo){
        List<WebChapterClassifyVo> chapterClassifyVo=new ArrayList<>();
        if(vo.getType()==0){
           chapterClassifyVo= cpsCreditService.selectChapter(Constants.CREDIT_COURSE);
        }else if(vo.getType()==1){
            chapterClassifyVo=cpsCreditService.selectKnowledge(Constants.CREDIT__SHARE);

        }else {
            chapterClassifyVo= cpsCreditService.selectMaterials(Constants.CREDIT_MATERIALS);

        }
        return success(chapterClassifyVo);
    }

    @PostMapping("/detailtKnowledge")
    public Object detailsCreditKnowledge(String dict){
        List<WebChapterClassifyVo> classifyVos=cpsCreditService.selectKnowledge(dict);
        return success(classifyVos);

    }

    @PostMapping("/detailMaterials")
    public Object detailsCreditMaterials(String dict){
        List<WebChapterClassifyVo> classifyVos= cpsCreditService.selectMaterials(dict);
        return  success(classifyVos);

    }*/

}
